<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>组织机构</title>
<jsp:include page="/WEB-INF/backstage/util.jsp"></jsp:include>
</head>
<body>
<form id="myForm" method="post">
<input type="hidden" name="cascade_id" value=${cascade_id }>
<div class="form-inline">
<div class="form-group">
<label for="dept_name">组织名称</label> <input type="text"
	class="form-control" id="dept_name" name="dept_name"
	placeholder="请输入组织名称" value="${dept_name }">
</div>
<button type="button" class="btn btn-primary" onclick="likeSysDept()">
	<span class="glyphicon glyphicon-search" id="refersh"></span>综合查询
</button>
<button type="button" class="btn btn-primary" data-toggle='modal'
	data-target='#myModal' id="dept_add" name="dept_add">
	<span class="glyphicon glyphicon-plus"></span>组织新增
</button>
</div>
</form>
	
	<div class="modal fade" id="myModal" tabindex="-1" role="dialog"
		aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						onclick="ClearVal()">
						<span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
					</button>
					<h4 class="modal-title" id="myModalLabel">管理员</h4>
				</div>
				<div class="modal-body">

					<form class="form-horizontal" id="form" method="post">
					<input type="hidden" name="dept_id2" id="dept_id2" value="0"> 
						<div class="form-group">
							<label for="username" class="col-sm-2 control-label">组织名称</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="dept_name2"
									name="dept_name" placeholder="请输入组织名称(必填)">
							</div>
						</div>
						 <div class="form-group" id="formdiv">
								<label for="dept_id" class="col-sm-2 control-label">上级组织</label>
								<div class="col-sm-10">
									<select class="form-control" id="dept_id" name="dept_id">
										
									</select>
								</div>
							</div>
							<div class="form-group">
							<label for="username" class="col-sm-2 control-label">负责人</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="manager"
									name="manager" placeholder="请输入负责人名称名称(必填)">
							</div>
						</div>
						<div class="form-group">
							<label for="username" class="col-sm-2 control-label">部门电话</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="phone"
									name="phone" placeholder="请输入电话号码">
							</div>
						</div>
						<div class="form-group">
							<label for="username" class="col-sm-2 control-label">地址</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="address"
									name="address" placeholder="请输入地址">
							</div>
						</div>
						<div class="form-group">
							<label for="username" class="col-sm-2 control-label">备注</label>
							<div class="col-sm-10">
								<textarea cols="60" rows="10" id="remark" name="remark"></textarea>
							</div>
						</div>
					</form>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default"  data-dismiss="modal"
					onclick="ClearVal()">关闭</button>
					<button type="button" class="btn btn-primary" 
						onclick="Save_SysDept()">保存</button>
				</div>
			</div>
		</div>
	</div>

	<table class="table-hover table-condensed table-bordered" width="100%">
		<tr>
			<th>编号</th>
			<th>组织名称</th>
			<th>负责人</th>
			<th>部门电话</th>
			<th>地址</th>
			<th>创建时间</th>
			<th>操作</th>
		</tr>
		 <c:forEach items="${list }" var="dept" varStatus="statu">
			<tr>
				<td>${statu.index+1 }</td>
				<td>${dept.dept_name}</td>
				<td>${dept.manager }</td>
				<td>${dept.phone }</td>
				<td>${dept.address }</td>
				<td><fmt:formatDate value="${dept.create_time }" pattern="yyyy-MM-dd"/></td>
				<td>
					<button type="button" class='btn btn-info' onclick="deleteSysDept(${dept.dept_id})">
				<span class='glyphicon glyphicon-remove'></span>删除</button>&nbsp;&nbsp;
				 <button type='button'  class='btn btn-info' data-toggle='modal' data-target='#myModal' 
				 onclick="updateSysDept(${dept.dept_id})"><span class='glyphicon glyphicon-pencil'></span>修改</button>
				</td>
			</tr>
		</c:forEach> 
	</table>
</body>
<script>
//清空
function ClearVal(){
	$("#dept_id2").val("0");
	$("#dept_name2").val("");
	$("#dept_id").val("-1");
	$("#manager").val("");
	$("#address").val("");
	$("#phone").val("");
	$("#remark").val("");
	$("#dept_id").attr("disabled", false);
} 

//修改
function updateSysDept(dept_id){
	$("#dept_id").attr("disabled", true);
	$.ajax({
		type:"post",
		url:"${pageContext.request.contextPath }/sysDept/updateSysDept",
		data:{"dept_id":dept_id},
		success:function(msg){
			var sys=eval('('+msg+')');
			$("#dept_id2").val(sys.dept_id);
			$("#dept_name2").val(sys.dept_name);
			$("#dept_id").val(sys.parent_id);
			$("#manager").val(sys.manager);
			$("#address").val(sys.address);
			$("#phone").val(sys.phone);
			$("#remark").val(sys.remark);
		}
	});
}	
	
//删除
function deleteSysDept(dept_id){
	$.ajax({
		type:"post",
		url:"${pageContext.request.contextPath }/sysDept/deleteSysDept",
		data:{"dept_id":dept_id},
		success:function(msg){
			parent.location.reload();
		}
	});
}

//保存
function Save_SysDept(){
	$.ajax({
		type:"post",
		url:"${pageContext.request.contextPath }/sysDept/saveSysDept",
		data:$("#form").serialize(),
		success:function(msg){
			parent.location.reload();
		}
	});
}
	
	//填充下拉框
	$(function(){
		$.ajax({
			type:'post',
			url:"${pageContext.request.contextPath }/user/selectSysDept",
			success:function(msg){
				$.each(msg,function(index,sysDept){
					/* var option="<option value="+sysDept.cascade_id+">"+sysDept.dept_name+"</option>"; */
					var option="<option value="+sysDept.dept_id+">"+sysDept.dept_name+"</option>";
					$("select").append(option);
				});
			}
		});
	});

	function likeSysDept(){
		myForm.action="${pageContext.request.contextPath }/sysDept/selectSysDept";
		myForm.submit();
	}
</script>
</html>